package com.zsy.utils;

import com.zsy.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-09-09 14:56
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Result ex(Exception e){
        e.printStackTrace();
//      log.info("运行异常方法ex：{}",e);
      return Result.error("服务器内部异常，请联系管理员");
    }
    @ExceptionHandler(value = DuplicateKeyException.class)
    public Result ex(DuplicateKeyException exception){
        exception.printStackTrace();
        log.info("运行异常方法ex：{}",exception);
        // 判断员工异常
        if (exception.getMessage().contains("emp.username")){
            return Result.error("员工的用户名重复");
        }else if (exception.getMessage().contains("emp.phone")){
            return Result.error("员工的手机号重复");
        }
        // 判断部门异常
        if (exception.getMessage().contains("dept.name"))
            return Result.error("部门名称重复");
        return Result.error("服务器内部异常，请联系管理员");
    }
}
